package com.xiaodou.mjf.web.console.core.controller;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xiaodou.mjf.application.core.manager.FinancingManager;
import com.xiaodou.mjf.application.core.service.FinancingAttributeService;
import com.xiaodou.mjf.application.core.service.FinancingService;
import com.xiaodou.mjf.application.core.service.FinancingStatisticsService;
import com.xiaodou.mjf.entity.core.Financing;
import com.xiaodou.mjf.entity.core.FinancingAttribute;
import com.xiaodou.mjf.infrastructure.common.components.Result;
import com.xiaodou.mjf.infrastructure.common.controller.ApplicationController;
import com.xiaodou.mjf.infrastructure.common.utils.DateUtils;
import com.xiaodou.mjf.infrastructure.common.utils.Global;

/**
 * 产品管理Controller
 * 
 * @author BILLY
 *
 */
@Controller
@RequestMapping("financing/meili")
public class MeiLiProductController extends ApplicationController {

	@Resource
	FinancingService financingService;
	@Resource
	FinancingStatisticsService financingStatisticsService;
	@Resource
	FinancingAttributeService financingAttributeService;
	@Resource
	FinancingManager financingManager;

	@RequestMapping("")
	public String xiaodou(Financing financing) {

		return "core/finance/meili/list";
	}

	@RequestMapping(value = { "data.json" })
	@ResponseBody
	public Map<String, Object> data(Financing financing) {
		try {
			financing.setType(Financing.TYPE_MEILI);
			Page<Financing> page = financingService.pageAll(financing);
			return page(page);
		} catch (Exception ex) {
			logger.error(ex.toString());
			throw ex;
		}

	}

	/**
	 * 产品添加
	 * 
	 * @param financing
	 * @param model
	 * @return
	 */
	@RequestMapping(value = { "form", "form/{id}" }, method = RequestMethod.GET)
	public String form(Financing financing, Model model) {

		return "core/finance/meili/form";
	}

	/**
	 * 保存
	 * 
	 * @param financing
	 * @param file
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = { "save" }, method = RequestMethod.POST)
	public String save(Financing financing, Model model) throws Exception {

		Result result = financingManager.meiliSave(financing);
		try {
			if (!result.isSuccess()) {
				return result.getMessage();
			}

		} catch (Exception ex) {
			logger.error(ex.toString());
			ex.printStackTrace();
			throw ex;
		}
		return "redirect:";
	}

	/**
	 * 修改
	 * 
	 * @param financing
	 * @param file
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = { "update", "update/{id}" }, method = RequestMethod.GET)
	public String update(@PathVariable String id, Model model) {

		try {
			Financing financing = financingService.get(id);
			FinancingAttribute attribute = financingAttributeService.get(financing.getId());
			if (attribute == null) {
				attribute = new FinancingAttribute();
			}
			model.addAttribute("financing", financing);
			model.addAttribute("attribute", attribute);

			String imgFtpPath = Global.getImgPath();
			model.addAttribute("imgFtpPath", imgFtpPath);
		} catch (Exception ex) {
			logger.error(ex.toString());
			throw ex;
		}
		return "core/finance/meili/update";
	}

	/**
	 * 产品删除
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = { "delete", "delete/{id}" })
	@ResponseBody
	public Map<String, Object> delete(@PathVariable String id, Model model) {
		try {
			Financing financing = financingService.get(id);
			if (financing != null) {
				financing.setStatus(Financing.DELETE);
				financingService.save(financing);
			}
		} catch (Exception ex) {
			logger.error(ex.toString());
			throw ex;
		}
		return map(true);
	}

	/**
	 * 产品下架
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = { "prodDown", "prodDown/{id}" })
	@ResponseBody
	public Map<String, Object> prodDown(@PathVariable String id, Model model) {
		Result result = new Result();
		Financing financing = financingService.get(id);
		if (financing == null) {
			result.setSuccess(false);
			result.setMessage("操作提示：数据不存在");
			return map(result);
		}
		Date currDate = DateUtils.getCurrentTime();
		Date endDate = financing.getEndTime();
		if (DateUtils.compare(currDate, endDate) == 1) {
			result.setSuccess(false);
			result.setMessage("操作提示：产品已过期");
			return map(result);
		}
		financing.setStatus(financing.getStatus() == Financing.INVEST_STATUS_SALE ? Financing.INVEST_STATUS_DOWN : financing.getStatus() == Financing.INVEST_STATUS_DOWN ? Financing.INVEST_STATUS_SALE : financing.getStatus());
		financingService.save(financing);
		result.setSuccess(true);
		result.setMessage("操作提示：执行成功");
		return map(result);
	}
}
